package net.easyjava.test.henanxi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.db.mysql.MysqlClient;
import net.easyjava.tools.http.HttpTools;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author ludi
 * @Date 2016 06 28 下午1:10
 */
public class GetData {
	//数据库相关
	public static final String  jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/crawler";
	public static final String  jdbcName = "juer";
	public static final String  jdbcPassword = "Juer2016";

	//梨园动态
	public static final String URL_LIYUANDONGTAI = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=6&page=";
	//资讯详情
	public static final String URL_LIYUANDONGTAI_DETAIL = "http://xqgb.newsfm.cc/api/index.php?nid=0&typeid=6&aid=";
	//广告图片,忽略
	public static final String URL_IGNORE = "http://zxqy.jizheweb.com/imager/xiaoxiang_logo.gif";
	//地址前缀
	public static final String URL_HOST = "http://xqgb.newsfm.cc";


	//唱段欣赏_豫剧 type=11
	public static final String URL_CHANGDUAN_YUJU = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=11&page=";
	//唱段欣赏_曲剧 type=12
	public static final String URL_CHANGDUAN_QUJU = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=12&page=";
	//唱段欣赏_越剧 type=13
	public static final String URL_CHANGDUAN_YUEJU = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=13&page=";
	//唱段欣赏_少数剧种 type=14
	public static final String URL_CHANGDUAN_SHAOSHU = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=14&page=";
	//唱段详情
	public static final String URL_CHANGDUAN_DETAIL = "http://xqgb.newsfm.cc/api/index.php?nid=1&typeid=11&aid=";


	//专辑列表
	public static final String URL_ALBUM_LIST = "http://xqgb.newsfm.cc/api/index.php?title=0&reid=4";
	//专辑曲目列表
	public static final String URL_ALBUM_AUDIO_LIST = "http://xqgb.newsfm.cc/api/index.php?column=99&page=0&aid=";
	//专辑曲目详情  %s 专辑id  %s 音频id
	public static final String URL_ALBUM_AUDIO_DETAIL = "http://xqgb.newsfm.cc/api/index.php?nid=1&typeid=%s&aid=%s";

	//伴奏列表 0-25页
	public static final String URL_ACCOMPANY_LIST = "http://xqgb.newsfm.cc/api/index.php?column=0&typeid=15&page=%s";


	public static void getArticle() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl,jdbcName,jdbcPassword);
			for(int i = 0 ; i<29 ; i++) {
				//获取第1页的信息,一共29页
				String s = HttpTools.get(URL_LIYUANDONGTAI + i);
				JSONObject jsonObject = JSONObject.parseObject(s);
				//每页10条文章,遍历。
				JSONArray data = jsonObject.getJSONArray("data");
				for (Object o : data) {
					JSONObject jsonObject1 = (JSONObject) o;
					//根据ID,获取文章详情
					String s2 = HttpTools.get(URL_LIYUANDONGTAI_DETAIL + jsonObject1.get("id"));
					Document parse = Jsoup.parse(s2);
					//文章内容
					String content = parse.text();
					jsonObject1.put("content", parse.text());
					//文章图片
					Elements body = parse.getElementsByTag("img");
					JSONArray imgArray = new JSONArray();
					for (Element element : body) {
						//去除最后的广告图
						if (URL_IGNORE.equals(element.attr("src"))) {
							continue;
						}
						imgArray.add(URL_HOST + element.attr("src"));
					}
					//包含所有文章配图的json串
					jsonObject1.put("contentUrl", imgArray.toJSONString());
					jsonObject1.put("frontUrl",URL_HOST+jsonObject1.get("litpic"));
					ArticleBean articleBean = JSONObject.parseObject(jsonObject1.toString(), ArticleBean.class);
					//保存文章
					saveArticle(client, articleBean);
				}
			}


		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}
	}

	private static void saveArticle(MysqlClient client, ArticleBean articleBean) {
		String sql = "INSERT INTO HNX_ARTICLE(id,title,description,front_url,writer,content,content_url) " +
				"VALUES(?,?,?,?,?,?,?)";
		try {
			client.execute(sql, articleBean.getId(), articleBean.getTitle(), articleBean.getDescription(), articleBean.getFrontUrl(),
                    articleBean.getWriter(), articleBean.getContent(), articleBean.getContentUrl());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void getChangDuan() throws IOException {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			//豫剧,i<55
//			String url = URL_CHANGDUAN_YUJU;
			//越剧,i<13
//			String url = URL_CHANGDUAN_YUEJU;
			//曲剧,i<5
//			String url = URL_CHANGDUAN_QUJU;
			//少数剧种,i<2
			String url = URL_CHANGDUAN_SHAOSHU;
			for (int i=0 ; i< 2; i++) {
				String s = HttpTools.get(url + i);
				JSONObject jsonObject = JSONObject.parseObject(s);
				JSONArray data = jsonObject.getJSONArray("data");
				System.out.println(data.size());
				for (Object o : data) {
					JSONObject jsonObject1 = (JSONObject) o;
					String s1 = HttpTools.get(URL_CHANGDUAN_DETAIL + jsonObject1.get("id"));
					System.out.println(jsonObject1.get("id"));
					Document parse = Jsoup.parse(s1);
					Elements elementsByClass = parse.getElementsByClass("page-view-article-content");
					Elements elementsByClass1 = parse.getElementsByClass("page-view-article-info");
					Elements elementsByClass2 = parse.getElementsByClass("page-view-article-title");
//					System.out.println(elementsByClass2.text() + elementsByClass1.text() + elementsByClass2.text());
					jsonObject1.put("content", elementsByClass2.text() + elementsByClass1.text() + elementsByClass.text());
					jsonObject1.put("mediaUrl",URL_HOST + jsonObject1.get("myradio"));
					jsonObject1.put("frontUrl",URL_HOST + jsonObject1.get("litpic"));
					//豫剧
//					jsonObject1.put("type", "11");
					//越剧
//					jsonObject1.put("type", "12");
					//曲剧
//					jsonObject1.put("type", "13");
					//其他剧种
					jsonObject1.put("type", "14");
					ChangDuanBean changDuanBean = JSONObject.parseObject(jsonObject1.toString(), ChangDuanBean.class);
					System.out.println(changDuanBean);
					saveChangDuan(client, changDuanBean);
					/***********************************************************************************
					 * 	增加专辑分类
					 * 	insert into HNX_ALBUM(id,title) VALUES(337,'经典豫剧唱段专辑');
					 *	insert into HNX_ALBUM(id,title) VALUES(338,'经典曲剧唱段专辑');
					 *	insert into HNX_ALBUM(id,title) VALUES(339,'经典越剧唱段专辑');
					 *	insert into HNX_ALBUM(id,title) VALUES(340,'其他唱段专辑');
					 *
					 *	更新所属专辑信息
					 *	update HNX_CHANGDUAN set album_id = 337 where type=11;
					 *	update HNX_CHANGDUAN set album_id = 338 where type=12;
					 *	update HNX_CHANGDUAN set album_id = 339 where type=13;
					 *	update HNX_CHANGDUAN set album_id = 340 where type=14;
					 *
					 */
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}


	}

	private static void saveChangDuan(MysqlClient client, ChangDuanBean changDuanBean) {
		String sql = "INSERT INTO HNX_CHANGDUAN(id,title,type,description,front_url,media_url,content)" +
				" VALUES(?,?,?,?,?,?,?)";
		try {
			client.execute(sql, changDuanBean.getId(), changDuanBean.getTitle(), changDuanBean.getType(), changDuanBean.getDescription(),
                    changDuanBean.getFrontUrl(), changDuanBean.getMediaUrl(), changDuanBean.getContent());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void getAlbum() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			String s = HttpTools.get(URL_ALBUM_LIST);
			JSONObject jsonObject = JSONObject.parseObject(s);
			//获取专辑列表
			JSONArray albumList = jsonObject.getJSONArray("data");
			for (Object o : albumList) {//遍历,获取每个专辑
				JSONObject album = (JSONObject) o;
				album.put("frontUrl",URL_HOST + album.get("litpic"));
				AlbumBean albumBean = JSONObject.parseObject(album.toString(), AlbumBean.class);
				saveAlbum(client, albumBean);
				JSONObject jsonObject1 = JSONObject.parseObject(HttpTools.get(URL_ALBUM_AUDIO_LIST+album.get("id")));
				JSONArray audioList = jsonObject1.getJSONArray("data");
				for (Object o1 : audioList) {//遍历每个专辑下的音频
					JSONObject audio = (JSONObject) o1;
					String audioDetailUrl = String.format(URL_ALBUM_AUDIO_DETAIL, album.get("id"),audio.get("id"));
					//解析html
					Document doc = Jsoup.parse(HttpTools.get(audioDetailUrl));
					//提取音频简介
					String content = doc.getElementsByClass("page-view-article-title").text()
							            + doc.getElementsByClass("page-view-article-info").text()
							            + doc.getElementsByClass("page-view-article-content").text();
					audio.put("content", content);
					audio.put("albumId", album.get("id"));
					audio.put("frontUrl", URL_HOST + album.get("litpic"));
					audio.put("audioUrl", URL_HOST + audio.get("myradio"));
					AlbumAudioBean albumAudioBean = JSONObject.parseObject(audio.toString(), AlbumAudioBean.class);
					saveAlbumAudio(client, albumAudioBean);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}
	}

	private static void saveAlbumAudio(MysqlClient client, AlbumAudioBean albumAudioBean) {
		String sql = "INSERT INTO HNX_ALBUM_AUDIO(id,title,writer,front_url,audio_url,content,album_id) " +
				     " VALUES(?,?,?,?,?,?,?)";
		try {
			client.execute(sql, albumAudioBean.getId(), albumAudioBean.getTitle(), albumAudioBean.getWriter(),
                           albumAudioBean.getFrontUrl(), albumAudioBean.getAudioUrl(), albumAudioBean.getContent(),
                           albumAudioBean.getAlbumId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static void saveAlbum(MysqlClient client, AlbumBean albumBean) {
		String sql = "INSERT INTO HNX_ALBUM(id,title,front_url) VALUES(?,?,?)";
		try {
			client.execute(sql, albumBean.getId(), albumBean.getTitle(), albumBean.getFrontUrl());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void getAccompany() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			for (int i=0;i<26;i++) {
				String url = String.format(URL_ACCOMPANY_LIST, i);
				JSONObject jsonObject = JSONObject.parseObject(HttpTools.get(url));
				JSONArray accompanys = jsonObject.getJSONArray("data");
				for (Object o : accompanys) {
					JSONObject accompany = (JSONObject) o;
					accompany.put("description",accompany.get("playsoperas"));
					accompany.put("frontUrl", URL_HOST + accompany.get("litpic"));
					accompany.put("audioUrl",URL_HOST + accompany.get("myradio"));
					AccompanyBean accompanyBean = JSONObject.parseObject(accompany.toString(), AccompanyBean.class);
					saveAccompany(client,accompanyBean);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}
	}

	private static void saveAccompany(MysqlClient client, AccompanyBean accompanyBean) {
		String sql = "INSERT INTO HNX_ACCOMPANY(id,title,description,front_url,audio_url) VALUES(?,?,?,?,?)";
		try {
			client.execute(sql, accompanyBean.getId(), accompanyBean.getTitle(), accompanyBean.getDescription(),
                    accompanyBean.getFrontUrl(), accompanyBean.getAudioUrl());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void get2() throws IOException {
		String s = HttpTools.get(URL_LIYUANDONGTAI_DETAIL+1503);
		System.out.println(s);
		Document parse = Jsoup.parse(s);
		System.out.println(parse.text());
		Elements body = parse.getElementsByTag("img");
		System.out.println(body.size());
		JSONArray imgArray = new JSONArray();
		for (Element element : body) {
			System.out.println(element.attr("src"));
			imgArray.add(URL_HOST + element.attr("src"));
		}
		System.out.println(imgArray.toJSONString());
	}


}
